﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;

namespace ENMAX_ArcGIS
{
    /// <summary>
    /// Interaction logic for SurfaceUserControl1.xaml
    /// </summary>
    public partial class TroublePinLayer : SurfaceUserControl
    {
        TwoPointScatterView tpScatterView;

        public TwoPointScatterView MapScatterLayer
        {
            get
            {
                return tpScatterView;
            }
            set
            {
                tpScatterView = value;
            }
        }

        public TroublePinLayer()
        {
            InitializeComponent();
        }

         public void AddPinToLayer(TroubleTicketPin ttp)
        {
            ttp.Style = Resources["TroublePin"] as Style;

            switch(ttp.Severity)
            {
                case TroubleTicket.TroubleTicketSeverity.High:
                    ttp.Background = new SolidColorBrush(Colors.Red);
                    break;
                case TroubleTicket.TroubleTicketSeverity.Medium:
                    ttp.Background = new SolidColorBrush(Colors.Yellow);
                    break;
                case TroubleTicket.TroubleTicketSeverity.Low:
                    ttp.Background = new SolidColorBrush(Colors.Green);
                    break;
                default:
                    ttp.Background = new SolidColorBrush(Colors.Gray);
                    break;

            }



            ttp.Center = new Point(ttp.Y, ttp.X);

            ttp.ContactTapGesture += new ContactEventHandler(pin_ContactTapGesture);
            ttp.ContactHoldGesture += new ContactEventHandler(pin_ContactHoldGesture);
            
             TroublePinCanvas.Items.Add(ttp);
             //preload the maptips
            TroublePinCanvas.Items.Add(ttp.MapTip);

            //SurfaceButton pin = new SurfaceButton();
            //pt.Style = Resources["TroublePin"] as Style;
            //pt.Background = new SolidColorBrush(Colors.Red);
            //pin.Style = new DynamicResourceExtension(trouble
            //pin.Fill = new SolidColorBrush(Colors.Red);
            //pin.Stroke = new SolidColorBrush(Colors.Black);
            //pin.StrokeThickness = 2;
            //pin.Width = 25;
            //pin.Height = 25;

            //Canvas.SetLeft(pin, pt.Y);
            //Canvas.SetTop(pin, pt.X);
            
            //pin.ContactTapGesture += new ContactEventHandler(pin_ContactTapGesture);
            //pin.ContactHoldGesture += new ContactEventHandler(pin_ContactHoldGesture);
            //TroublePinCanvas.Children.Add(pin);
        }

         void pin_ContactHoldGesture(object sender, ContactEventArgs e)
         {
             TroubleTicketPin tappedPin = sender as TroubleTicketPin;
             TroubleTicket tappedTrouble = tappedPin.TroubleTicket;

             MapScatterLayer.CreateMapFrameFromTroublePin(tappedTrouble, new Point(768-tappedPin.X, tappedPin.Y), e.Contact.GetOrientation(MapScatterLayer));
         }

         private void pin_ContactTapGesture(object sender, ContactEventArgs e)
         {
             TroubleTicketPin tappedPin = sender as TroubleTicketPin;

             //ToolTip tip = new ToolTip();
             //tip.IsOpen = true;
             //tip.Height = 200;
             //tip.Width = 300;
             //tip.Background = new SolidColorBrush(Colors.White);
             //tip.Content = "This is a tip";
             //tappedPin.ToolTip = tip;
             tappedPin.MapTip.Center = new Point(tappedPin.Y, tappedPin.X);
             tappedPin.MapTip.Orientation = e.Contact.GetOrientation(this);

             if (tappedPin.MapTip.Visibility != Visibility.Visible)
             {
                 tappedPin.MapTip.IsActive = true;
                 tappedPin.MapTip.Visibility = Visibility.Visible;
                 return;
             }

             //ToolTipService.SetIsEnabled(tappedPin, true);




             //TextBlock svi = new TextBlock();

             //svi.Width = 100;
             //svi.Height = 50;
             //svi.Text = "TEST";
             //Canvas.SetTop(svi, tappedPin.X);
             //Canvas.SetLeft(svi, tappedPin.Y);

             //svi.LayoutTransform = new RotateTransform(e.Contact.GetOrientation(this)+90);

             //Console.WriteLine("Touch orie = " + e.Contact.GetOrientation(this));
             
             //svi.Background = new SolidColorBrush(Colors.White);
             //svi.Foreground = new SolidColorBrush(Colors.Black);

             //ArcGisMapTip tmp = tappedPin.GetToolTip();
             //Point touchPt = e.GetPosition(this);
             //double orientation = e.Contact.GetOrientation(TroublePinCanvas);

             //Point displayPt = new Point(0, 0);
             //double radianAngle = Math.PI * orientation / 180.0;
             //int distanceFromTouchPt = 100;

             //displayPt.X = (int)Math.Round(touchPt.X + distanceFromTouchPt * Math.Sin(radianAngle));
             //displayPt.Y = (int)Math.Round(touchPt.Y + distanceFromTouchPt * Math.Sin(radianAngle));

             ////tmp.LayoutTransform = new RotateTransform(e.Contact.GetOrientation(this));
             //Console.WriteLine("Tapped pin x = " + tappedPin.X + " y = " + tappedPin.Y);

             //TextBlock tb1 = new TextBlock();
             //tb1.Width = 100;
             //tb1.Height = 100;
             //tb1.Background = new SolidColorBrush(Colors.Red);
             //tb1.Text = "Left 100, Top 100";
             //tb1.LayoutTransform = new RotateTransform(90);
             //Canvas.SetLeft(tb1, 100);
             //Canvas.SetTop(tb1, 500);
             //TroublePinCanvas.Children.Add(tb1);

             //if (orientation >= 0 && orientation < 90)
             //{
             //    tmp.LayoutTransform = new RotateTransform(180);
             //    Canvas.SetLeft(tmp, tappedPin.Y + 30);
             //    Canvas.SetTop(tmp, tappedPin.X + 30);
             //}
             //if (orientation > 90 && orientation <= 180)
             //{
             //    tmp.LayoutTransform = new RotateTransform(270);
             //    Canvas.SetLeft(tmp, tappedPin.Y - tmp.Height - 10);
             //    Canvas.SetTop(tmp, tappedPin.X + 10);
             //}
             //if (orientation > 180 && orientation <= 270)
             //{
             //    tmp.LayoutTransform = new RotateTransform(0);
             //    Canvas.SetLeft(tmp, tappedPin.Y - tmp.Width - 10);
             //    Canvas.SetTop(tmp, tappedPin.X - tmp.Height - 10);
             //}
             //if (orientation > 270 && orientation < 360)
             //{
             //    tmp.LayoutTransform = new RotateTransform(90);
             //    Canvas.SetLeft(tmp, tappedPin.Y + 10);
             //    Canvas.SetTop(tmp, tappedPin.X - tmp.Width - 10);
             //}

             
         }

         public void RemoveAllPins()
         {
             TroublePinCanvas.Items.Clear();
         }

    }
}
